package com.dreamlin.gankio.mvvm.util

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import coil.api.load
import coil.transform.RoundedCornersTransformation
import com.dreamlin.gankio.common.dp2px
import com.dreamlin.gankio.mvvm.home.GankEntity
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState

object BindingAdapters {

    @BindingAdapter("app:refreshState")
    fun refreshState(refreshLayout: SmartRefreshLayout, state: RefreshState) {
        when (state) {
            RefreshState.LoadFinish -> {
                refreshLayout.finishLoadMore()
            }
            RefreshState.RefreshFinish -> {
                refreshLayout.finishLoadMore()
            }
        }
    }

    @BindingAdapter("app:imageUrl")
    fun loadImage(imageView: ImageView, url: String) {
        imageView.load(url) {
            crossfade(true)
        }
    }

    @BindingAdapter("app:fadeUrl")
    fun fade(imageView: ImageView, url: String) {
        imageView.load(url) {
            crossfade(true)
        }
    }

    @BindingAdapter("app:fadeRoundUrl")
    fun fadeRoundIcons(imageView: ImageView, url: String) {
        imageView.load(url) {
            crossfade(true)
            transformations(RoundedCornersTransformation(5f.dp2px()))
        }
    }

    @BindingAdapter("app:data")
    fun initAdapter(
        recyclerView: RecyclerView,
        data: List<GankEntity>
    ) {
        recyclerView.adapter?.let {

        }
    }

}